package com.atguigu.ssyx.acl.service.impl;

import com.atguigu.ssyx.acl.mapper.PermissionMapper;
import com.atguigu.ssyx.acl.service.PermissionRoleService;
import com.atguigu.ssyx.acl.service.PermissionService;
import com.atguigu.ssyx.acl.utils.PermissionHelper;
import com.atguigu.ssyx.model.acl.Permission;
import com.atguigu.ssyx.model.acl.RolePermission;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Description:
 * Author Xu Chen xu
 * Create 2024/4/24 - 下午2:59
 * version v1.0
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {
    @Autowired
    private PermissionRoleService permissionRoleService;

    //查询所有菜单

    @Override
    public List<Permission> consultAllMenus() {
        List<Permission> list = baseMapper
                .selectList(new QueryWrapper<Permission>()
                        .orderByAsc("CAST(id AS SIGNED)"));
        return PermissionHelper.buildPermission(list);
    }

    @Override
    public boolean removeChildById(Long id) {
        //1、创建集合idList，封装所有要删除的菜单id
        List<Long> idList = new ArrayList<>();
        //2、根据当前菜单id，获取当前菜单下面所有子菜单，
        //如果子菜单下面还有子菜单，都要获取的到
        //重点：递归获取当前菜单下面的子菜单
        this.getAllPermissionId(id, idList);
        idList.add(id);
        //调用方法根据多个菜单id删除
        baseMapper.deleteBatchIds(idList);
        return true;
    }

    //重点：递归找当前菜单下面所有子菜单
    //id：当前菜单id
    //idList：封装所有要删除的菜单id
    private void getAllPermissionId(Long id, List<Long> idList) {
        //根据当前菜单id，查询子菜单
        LambdaQueryWrapper<Permission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Permission::getPid, id);
        List<Permission> childList = baseMapper.selectList(wrapper);
        //递归查询是否还有子菜单，有继续递归查询
        childList.stream()
                .forEach(item -> {
            //封装id到idList集合里面
            idList.add(item.getId());
            //递归查询
            this.getAllPermissionId(item.getId(), idList);
        });
    }

    @Override
    public List<Permission> findPermissionById(Long roleId) {
        List<Permission> allPermissionList = baseMapper.selectList(null);

        List<RolePermission> permissionList = permissionRoleService.list(new QueryWrapper<RolePermission>()
                .eq("role_id", roleId)
                .select("permission_id"));
        List<Long> exitPermissionIdList = permissionList.stream()
                .map(RolePermission::getPermissionId)
                .collect(Collectors.toList());

        List<Permission> assignPermission = new ArrayList<>();
        for (Permission permission : allPermissionList) {
            if (exitPermissionIdList.contains(permission.getId())) {//遍历所有菜单，判断当前菜单是否包含在已分配菜单集合中
                assignPermission.add(permission);
            }
        }
        return assignPermission;
    }

    @Override
    public void saveUserRoleRelationShip(Long roleId, Long[] permissionId) {
        permissionRoleService.remove(new QueryWrapper<RolePermission>().eq("role_id", roleId));

        List<RolePermission> rolePermissionList = new ArrayList<>();
        for (Long id : permissionId) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId(id);
            rolePermissionList.add(rolePermission);
        }
        permissionRoleService.saveBatch(rolePermissionList);
    }
}
